import allure
import pytest
import requests
import time
from testdata import enums, tyturl
from requests import session
from utils import getter

session = requests.Session()
url = getter.getUrl("getCBAStockBondBalanceIntervalFig")
header = getter.getHeader()


@allure.parent_suite('基金产品')
@allure.suite('基金产品-债券分析')
@allure.sub_suite('测试结果')
@allure.tag('基金产品-债券分析')
@allure.epic('投研通接口测试-基金产品')
@allure.feature('基础信息')
@allure.title('基金产品-债券分析-转债分析-区间持仓分布-债券余额/平价底价溢价率分布/转股价值分布-图')
@pytest.mark.system
@pytest.mark.run(order=3)
@pytest.mark.parametrize("fundcode",data.code_list)
@pytest.mark.parametrize("t",(lambda x: x[3:6]+[x[8]])(data.time_interval_options))
@pytest.mark.parametrize("distributionType",["1","2","3"])
def test_get_cba_stock_bond_balance_interval_fig(fundcode,t,distributionType):

    body = {
            "FundCode": fundcode,
            "asc":"false",
            "sortby":"",
            "time": t,
            "distributionType": distributionType
    }
    r = session.post(url = url,headers = header,json = body)
    r.encoding = 'utf-8'
    response = r.json()

    # 断言
    assert response["ErrorCode"] == 0, "ErrorCode正确"

    fields = ["pctType","pctnv"]
    values = {field:[] for field in fields}

    for dic in response["Data"]:
        if "pctnvInfos" in dic:
            for item in dic["pctnvInfos"]:
                for field in fields:
                    if field in item:
                        values[field].append(item[field])

    for field in fields:
        assert not all(value == "--" for value in values[field]),f"所有{field}值都是'--'，数据可能有误"

    # 间隔1秒
    time.sleep(1)